//
//  XcodeCleanHomeVM.swift
//  ZJTools
//
//  Created by 万孟超 on 2023/11/17.
//

import Foundation

class XcodeCleanHomeVM {
    
    struct CleanListType: OptionSet {
        let rawValue: Int
        
        static let derivedData = CleanListType(rawValue: 1 << 0)
        static let cache = CleanListType(rawValue: 1 << 1)
        static let archive = CleanListType(rawValue: 1 << 2)
        static let deviceSupport = CleanListType(rawValue: 1 << 3)
        static let simulator = CleanListType(rawValue: 1 << 4)
        
        static let all: CleanListType = [.derivedData, .cache, .archive, .deviceSupport, .simulator]
    }
    
    init() { }
    
    var cleanListType: CleanListType = [.derivedData]
    
    func isContains(_ index: Int) -> Bool {
        if index == 0 {
            return cleanListType.contains(.derivedData)
        }else if index == 1 {
            return cleanListType.contains(.cache)
        }else if index == 2 {
            return cleanListType.contains(.archive)
        }else if index == 3 {
            return cleanListType.contains(.deviceSupport)
        }else {
            return cleanListType.contains(.simulator)
        }
    }
    
    func freshListType(_ index: Int, isAdd: Bool) {
        if isAdd {
            appendListType(index)
        }else{
            removeListType(index)
        }
    }
    
    func appendListType(_ index: Int) {
        if isContains(index) { return }
        if index == 0 {
            cleanListType.update(with: .derivedData)
        }else if index == 1 {
            cleanListType.update(with: .cache)
        }else if index == 2 {
            cleanListType.update(with: .archive)
        }else if index == 3 {
            cleanListType.update(with: .deviceSupport)
        }else {
            cleanListType.update(with: .simulator)
        }
    }
    
    func removeListType(_ index: Int) {
        if !isContains(index) { return }
        if index == 0 {
            cleanListType.remove(.derivedData)
        }else if index == 1 {
            cleanListType.remove(.cache)
        }else if index == 2 {
            cleanListType.remove(.archive)
        }else if index == 3 {
            cleanListType.remove(.deviceSupport)
        }else {
            cleanListType.remove(.simulator)
        }
    }
    
    func cleanData(freshTable: @escaping (Int) -> ()) {
        
        if cleanListType.contains(.derivedData) {
            let model = XcodeCleanManager.shared.deriveDataModel
            for subPath in model.subPath {
                let path = model.path + "/" + subPath.path
                File.removePath(path) { isRemove in
                    XcodeCleanManager.shared.deriveDataModel.size -= subPath.size
                    XcodeCleanManager.shared.deriveDataModel.subPath.removeAll(where: {$0.path == subPath.path})
                    NotiManager.post(.XcodeCleanReceive, userInfo: ["index": 1, "size": XcodeCleanManager.shared.deriveDataModel.size, "isFreshFloder" : false])
                        .post(.XcodeCleanReceive, userInfo: ["index": 1, "isFreshFloder" : true])
                    freshTable(1)
                }
            }
        }else if cleanListType.contains(.cache) {
            let model = XcodeCleanManager.shared.cacheModel
            for subPath in model.subPath {
                let path = model.path + "/" + subPath.path
                File.removePath(path) { isRemove in
                    XcodeCleanManager.shared.cacheModel.size -= subPath.size
                    XcodeCleanManager.shared.cacheModel.subPath.removeAll(where: {$0.path == subPath.path})
                    NotiManager.post(.XcodeCleanReceive, userInfo: ["index": 2, "size": XcodeCleanManager.shared.cacheModel.size, "isFreshFloder" : false])
                        .post(.XcodeCleanReceive, userInfo: ["index": 2, "isFreshFloder" : true])
                    freshTable(2)
                }
            }
        }else if cleanListType.contains(.archive) {
            let model = XcodeCleanManager.shared.archiveModel
            for subPath in model.subPath {
                let path = model.path + "/" + subPath.path
                File.removePath(path) { isRemove in
                    XcodeCleanManager.shared.archiveModel.size -= subPath.size
                    XcodeCleanManager.shared.archiveModel.subPath.removeAll(where: {$0.path == subPath.path})
                    NotiManager.post(.XcodeCleanReceive, userInfo: ["index": 3, "size": XcodeCleanManager.shared.archiveModel.size, "isFreshFloder" : false])
                        .post(.XcodeCleanReceive, userInfo: ["index": 3, "isFreshFloder" : true])
                    freshTable(3)
                }
            }
        }else if cleanListType.contains(.deviceSupport) {
            let model = XcodeCleanManager.shared.deviceSupportModel
            for subPath in model.subPath {
                let path = model.path + "/" + subPath.path
                File.removePath(path) { isRemove in
                    XcodeCleanManager.shared.deviceSupportModel.size -= subPath.size
                    XcodeCleanManager.shared.deviceSupportModel.subPath.removeAll(where: {$0.path == subPath.path})
                    NotiManager.post(.XcodeCleanReceive, userInfo: ["index": 4, "size": XcodeCleanManager.shared.deviceSupportModel.size, "isFreshFloder" : false])
                        .post(.XcodeCleanReceive, userInfo: ["index": 4, "isFreshFloder" : true])
                    freshTable(4)
                }
            }
        }else if cleanListType.contains(.simulator) {
            let model = XcodeCleanManager.shared.simulatorModel
            for subPath in model.subPath {
                let path = model.path + "/" + subPath.path
                File.removePath(path) { isRemove in
                    XcodeCleanManager.shared.simulatorModel.size -= subPath.size
                    XcodeCleanManager.shared.simulatorModel.subPath.removeAll(where: {$0.path == subPath.path})
                    NotiManager.post(.XcodeCleanReceive, userInfo: ["index": 5, "size": XcodeCleanManager.shared.simulatorModel.size, "isFreshFloder" : false])
                        .post(.XcodeCleanReceive, userInfo: ["index": 5, "isFreshFloder" : true])
                    freshTable(5)
                }
            }
        }
    }
}
